<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Addmincont extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url'));
		$this->load->library('session');
		$this->load->library("form_validation");
		$this->load->model('admin_model');
		$this->load->library("my_layout");
		$this->my_layout->setLayout("layout/layout_admin");
	}
	private $path = './public/images/';
	private $limit = 20;
	public function checkLogin(){
		
		if ($this->checkSessionData()){
			if ($this->session->userdata('loginadmin') && md5('adminThanhNV') == $this->session->userdata('level')){
				//$this->my_layout->view('admin/index');
				return true;//login thanh cong
			}else{
				//validation data input
				$this->form_validation->set_rules('user_id', 'Username','trim|required|min_length[6]|max_length[20]|xss_clean');
				$this->form_validation->set_rules('pass_id', 'Password','trim|required|min_length[6]|max_length[20]|xss_clean');
				$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
				if($this->form_validation->run()){
					if ($this->checkUser($this->input->post('user_id'))){
						if ($this->checkPass($this->input->post('user_id'),$this->input->post('pass_id'))){
							$this->session->set_userdata('loginadmin',true);
							$this->session->set_userdata('level',md5('adminThanhNV'));
							//$this->my_layout->view('admin/index');
							return true;//login thanh cong
						}else{
							$data['error'] = '<font color = "red">Password not match, Please try again </font>';
							//$this->my_layout->view('admin/login',$data);
							return false;
						}
					}else{
						$data['error'] = '<font color = "red">User not exist, Please try again </font>';
						
						//$this->my_layout->view('admin/login',$data);
						return false;
					}
				}else{
					//data nhap vao ko hop le goi lai login
					//$this->my_layout->view('admin/login');
					return false;
				}
			}
		}else{
			//goi login
			$this->session->set_userdata('loginadmin','');
			return false;
			//$this->my_layout->view('admin/login');
		}
	}
	private function checkUser($user = null){
		//goi vao model kiem tra user co ton tai ko?
		$usermodel = 'TonyChau';
		$user = trim($user);
		if ($user == $usermodel){
			return true;
		}else return false;
	}
	private function checkPass($user = null, $pass = null){
		//goi vao model kiem tra user duoc truyen vao co dung pass nhap vo ko?
		$user = trim($user);
		$pass = trim($pass);
		$passmodel = md5('ThanhNV123456Ha');
		$usermodel = 'TonyChau';
		if ($user == $usermodel && $passmodel==md5('ThanhNV'.$pass)){
			return true;
		}else return false;
	}
	private function checkSessionData(){
		if (isset($this->session->userdata['loginadmin'])){
			return true;
		}else{
			return false;
		}
	}
	public function index(){
		if ($this->checkLogin()){
			$this->my_layout->view('admin/index');
		}
		else{
			$this->my_layout->view('admin/login');
		}
	}
	public function addProduct(){
		if($this->checkLogin()){
			
			$data = $this->admin_model->getType();
			$couttype = count($data);
			for($i=0;$i<$couttype;$i++){
				$temp = $this->admin_model->getTypeSub($data[$i]['cat_type_id']);
				$data[$i][]=$temp;
			}
			$setview['catetype']=$data;
			$setview['error'] ='';
			$this->my_layout->view('admin/addproduct',$setview);
		}
	}
	public function addProductMember(){
		if($this->checkLogin()){
			
			$setview['error'] ='';
			$this->my_layout->view('admin/addproductmember',$setview);
		}
	}
	public function addCatagory(){
		
		if($this->checkLogin()){
			
			$this->my_layout->view('admin/addcatagory',array('error' => ' ' ));
		}
	}
	public function addCateReceive() {
		if($this->checkLogin()){
			$this->load->helper('date');
			$data=array('cat_type_name'			=>$this->input->post('namecate'),
						'cat_type_code'			=>$this->input->post('codecate'),
						'cat_sort_order'		=>$this->input->post('sortcate'),
						'cat_type_delete_flag'	=>0,
						'update_date'			=>date('Y-m-d H:i:s',now())
			);
			$name = trim($this->input->post('namecate'));
			if (empty($name)){
				$setview['error']='Category Name Not Null';
			}else{
				$flag_exist = $this->admin_model->checkExistCat($name);
				if ($flag_exist>0){
					$setview['error']='Category Name Exist DB';
				}else {
					$this->admin_model->saveCate($data);
					$setview['error']='Add category Successfull';
				}
			}
			$this->my_layout->view('admin/addsuccess',$setview);
		}
	}
	public function addCatagorySub(){
		
		if($this->checkLogin()){
			
			$data = $this->admin_model->getType();
			$setview['catetype']=$data;
			$setview['error'] ='';
			$this->my_layout->view('admin/addcatagorysub',$setview);
		}
	}
	public function changImgBanner(){
		if($this->checkLogin()){
			$data['images'] = $this->admin_model->getImagesEdit();
			$data['error'] = '';
			$this->my_layout->view('admin/changimgbanner',$data);
		}
	}
	public function editImg($ref=null){
		if($this->checkLogin()){
			$data['error']='';
			$data['imgid'] = $ref;
			$this->my_layout->view('admin/editimg',$data);
		}
	}
	public function searchEditProductCommon(){
		if($this->checkLogin()){
			
			$data = $this->admin_model->getType();
			$couttype = count($data);
			for($i=0;$i<$couttype;$i++){
				$temp = $this->admin_model->getTypeSub($data[$i]['cat_type_id']);
				$data[$i][]=$temp;
			}
			$setview['catetype']=$data;
			$setview['error'] ='';
			$this->my_layout->view('admin/searcheditproduct',$setview);
		}
	}
	public function searchEditProductMember(){
		if($this->checkLogin()){
			
			$setview['error'] ='';
			$this->my_layout->view('admin/searcheditproductmember',$setview);
		}
	}
	public function searchEdit(){
		if($this->checkLogin()){
			$this->load->helper('url');		
			//Get Offset		
			if($this->uri->segment(6) == FALSE)
				$offset = 0;
			else
				$offset = $this->uri->segment(6);
			//Declare CodeIgniter Pagination
			$this->load->library('pagination');
			$name = ($this->input->post('namep')=='')?$this->uri->segment(3):trim($this->input->post('namep'));
			$color = ($this->input->post('cat_color')=='')?$this->uri->segment(4):trim($this->input->post('cat_color'));
			$type = ($this->input->post('typep')=='')?$this->uri->segment(5):trim($this->input->post('typep'));
			$data = array(
				'cat_name' 			=> ($name=='contsearch')?'':$name,
				'cat_color' 		=> ($color=='contsearch')?'':$color,
				'cat_type_sub_id' 	=> ($type=='contsearch')?'':$type
			);
			//Load data		
			$setview['common'] = $this->admin_model->searchCommonEdit($data,$this->limit,$offset);
			if ($name =='')$name = 'contsearch';
			if ($color =='')$color = 'contsearch';
			if ($type =='')$type = 'contsearch';
			$config['base_url'] = site_url('addmincont/searchEdit/'.$name.'/'.$color.'/'.$type.'/');
			
	 		$total = $this->admin_model->count_condition($data);
	 		
	 		$config['total_rows'] = $total;
	 		$config['per_page'] = $this->limit;
	 		$config['uri_segment'] = 6;
			$this->pagination->initialize($config);
			$setview['condition'] = $data;
			$this->my_layout->view('admin/searcheditresult',$setview);
		}
	}
	public function searchEditMember(){
		if($this->checkLogin()){
			$this->load->helper('url');		
			//Get Offset		
			if($this->uri->segment(6) == FALSE)
				$offset = 0;
			else
				$offset = $this->uri->segment(6);
			//Declare CodeIgniter Pagination
			$this->load->library('pagination');
			$name = ($this->input->post('namep')=='')?$this->uri->segment(3):trim($this->input->post('namep'));
			$color = ($this->input->post('cat_color')=='')?$this->uri->segment(4):trim($this->input->post('cat_color'));
			$type = ($this->input->post('typep')=='')?$this->uri->segment(5):trim($this->input->post('typep'));
			$data = array(
				'cat_name' 			=> ($name=='contsearch')?'':$name,
				'cat_color' 		=> ($color=='contsearch')?'':$color,
				'cat_type_sub_id' 	=> ($type=='contsearch')?'':$type
			);
			//Load data		
			$setview['common'] = $this->admin_model->searchMemberEdit($data,$this->limit,$offset);
			if ($name =='')$name = 'contsearch';
			if ($color =='')$color = 'contsearch';
			if ($type =='')$type = 'contsearch';
			$config['base_url'] = site_url('addmincont/searchEditMember/'.$name.'/'.$color.'/'.$type.'/');
			
	 		$total = $this->admin_model->count_conditionMember($data);
	 		
	 		$config['total_rows'] = $total;
	 		$config['per_page'] = $this->limit;
	 		$config['uri_segment'] = 6;
			$this->pagination->initialize($config);
			$setview['condition'] = $data;
			$this->my_layout->view('admin/searcheditresultmember',$setview);
		}
	}
	
	function do_upload(){
		if($this->checkLogin()){
			$temp = $this->input->post('typep');
			$tempimg = explode("_", $temp);
			$pathimg = $this->setPathImg($tempimg[1]);
			$subtype_id = $tempimg[0];
			$config['upload_path'] = $this->path.$pathimg;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
	
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if (! $this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
	
				$this->my_layout->view('admin/addproduct',$error);
			}
			else{
				$this->load->helper('date');
				$data1 = array('upload_data' => $this->upload->data());
				
				$data = array(
								'cat_name' => $this->input->post('namep'),
								'cat_code' => $this->input->post('codep'),
								'cat_price' => $this->input->post('pricep'),
								'cat_type' => 0,
								'cat_description' => $this->input->post('descriptionp'),
								'cat_demension' => $this->input->post('demensionp'),
								'cat_assembly' => $this->input->post('assemblyp'),
								'cat_material' => $this->input->post('materialp'),
								'cat_color' => $this->input->post('colorp'),
								'update_date' => date('Y-m-d H:i:s',now()),
								'delete_flag' => 0,
								'cat_note' => '',
								'cat_type_sub_id' => $subtype_id,
								'images'		=> $pathimg.'/'.$data1['upload_data']['file_name'],
								'cat_type_sub_sort_order' => $this->input->post('sortp'),
								'cat_size' => $this->input->post('sizep')
				);
				$this->admin_model->saveCatSubProduct($data);
				$setview['error']='Add product';
				$this->my_layout->view('admin/addsuccess', $setview);
			}
		}
	}
	function do_upload_Member(){
		if($this->checkLogin()){
			$subtype_id = $this->input->post('typep');
			if ($subtype_id == 11)$pathimg = 'newproduct';
			elseif ($subtype_id == 12)$pathimg = 'newdesign';
			elseif ($subtype_id == 13)$pathimg = 'store';
			else $pathimg='';
			$config['upload_path'] = $this->path.$pathimg;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
	
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if (! $this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
	
				$this->my_layout->view('admin/addproductmember',$error);
			}
			else{
				$this->load->helper('date');
				$data1 = array('upload_data' => $this->upload->data());
				
				$data = array(
								'cat_name' => $this->input->post('namep'),
								'cat_code' => $this->input->post('codep'),
								'cat_price' => $this->input->post('pricep'),
								'cat_type' => 0,
								'cat_description' => $this->input->post('descriptionp'),
								'cat_demension' => $this->input->post('demensionp'),
								'cat_assembly' => $this->input->post('assemblyp'),
								'cat_material' => $this->input->post('materialp'),
								'cat_color' => $this->input->post('colorp'),
								'update_date' => date('Y-m-d H:i:s',now()),
								'delete_flag' => 0,
								'cat_note' => '',
								'cat_type_sub_id' => $subtype_id,
								'images'		=> $pathimg.'/'.$data1['upload_data']['file_name'],
								'cat_type_sub_sort_order' => $this->input->post('sortp'),
								'cat_size' => $this->input->post('sizep')
				);
				$this->admin_model->saveCatSubProductMember($data);
				$setview['error']='Add product Member';
				$this->my_layout->view('admin/addsuccess', $setview);
			}
		}
	}
	function do_uploadcatsub(){
		if($this->checkLogin()){
			$temp = $this->input->post('typep');
			$tempimg = explode("_", $temp);
			$subtype_id = $tempimg[0];
			$config['upload_path'] = $this->path.'imgcommon/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
	
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if (! $this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
				$data = $this->admin_model->getType();
				$error['catetype']= $data;
	
				$this->my_layout->view('admin/addcatagorysub',$error);
			}
			else{
				$this->load->helper('date');
				$data1 = array('upload_data' => $this->upload->data());
				$name = trim($this->input->post('catename'));
				$type = $this->input->post('typep');
				$data = array(
								'cat_type_sub_name' => $name,
								'cat_type_sub_code' => $this->input->post('catecode'),
								'cat_type_sub_sort_order' => $this->input->post('catesort'),
								'cat_type_sub_delete_flag' => 0,
								'cat_type_id' => $type,
								'update_date' => date('Y-m-d H:i:s',now()),
								'images' => $data1['upload_data']['file_name']
				);
				if (empty($name)||empty($type)){
					$setview['error']='Error Sub Category Name and Category Type Not Null';
				}else{
					$flag_exist = $this->admin_model->checkExistSubcat($name,$type);
					if ($flag_exist>0){
						$setview['error']='Error Sub Categories Name Exist DB';
					}else{
						$this->admin_model->saveCateSub($data);
						$setview['error']='Add Subcategory Successfull';
					}
				}
				$this->my_layout->view('admin/addsuccess', $setview);
			}
		}
	}
	public function logout(){
		$this->session->set_userdata('loginadmin',false);
		$this->session->set_userdata('level','');
		$this->index();
	}
	private function setPathImg($str = ''){
		$result = '';
		switch(strtoupper($str)) {
			case "LIVINGROOM":
				$result = 'livingroom';
				break;
			case "DININGROOM":
				$result = 'dinningroom';
				break;
			case "BEDROOM":
				$result = 'bedroom';
				break;
			case "OFFICE":
				$result = 'livingroom';
				break;
			case "ENTERTAINMENT":
				$result = 'entertainment';
				break;
			case "KIDS":
				$result = 'kids';
				break;
			case "OUTDOOR":
				$result = 'outdoor';
				break;
			case "OTHERS":
				$result = 'others';
				break;
		}
		return $result;
	}
	public function changeImage(){
		if($this->checkLogin()){
			$config['upload_path'] = $this->path.'slide/';
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
	
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if (! $this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
	
				$this->my_layout->view('admin/editimg',$error);
			}
			else{
				$data1 = array('upload_data' => $this->upload->data());
				$data = array(
								'id' => $this->input->post('imgid'),
								'delete_flag' => 0,
								'images' => $data1['upload_data']['file_name']
				);
				$this->admin_model->updateImg($data);
				$setview['error']='Update images';
				$this->my_layout->view('admin/addsuccess', $setview);
			}
		}
	}
	public function editProductC($ref = NULL){
		if($this->checkLogin()){
			$data = $this->admin_model->searchPId($ref);
			$setview['detail'] = $data[0];
			$this->my_layout->view('admin/editproductdetail',$setview);
		}
	}
	public function editProductM($ref = NULL){
		if($this->checkLogin()){
			$data = $this->admin_model->searchPIdM($ref);
			$setview['detail'] = $data[0];
			$this->my_layout->view('admin/editproductdetailm',$setview);
		}
	}
	public function updateProduct(){
		if($this->checkLogin()){
			$this->load->helper('date');
			$id = $this->input->post('catid');
			$data = array(
						'cat_name'					=>$this->input->post('name'),
						'cat_code'					=>$this->input->post('code'),
						'cat_price'					=>$this->input->post('price'),
						'cat_type_sub_sort_order'	=>$this->input->post('sort'),
						'cat_description'			=>$this->input->post('description'),
						'cat_demension'				=>$this->input->post('demension'),
						'cat_assembly'				=>$this->input->post('assembly'),
						'cat_material'				=>$this->input->post('material'),
						'cat_color'					=>$this->input->post('color'),
						'cat_size'					=>$this->input->post('size'),
						'update_date'				=> date('Y-m-d H:i:s',now())
			);
			$this->admin_model->updateProductId($data,$id);
			redirect('addmincont/index');
		}
	}
	public function updateProductM(){
		if($this->checkLogin()){
			$this->load->helper('date');
			$id = $this->input->post('catid');
			$data = array(
						'cat_name'					=>$this->input->post('name'),
						'cat_code'					=>$this->input->post('code'),
						'cat_price'					=>$this->input->post('price'),
						'cat_type_sub_sort_order'	=>$this->input->post('sort'),
						'cat_description'			=>$this->input->post('description'),
						'cat_demension'				=>$this->input->post('demension'),
						'cat_assembly'				=>$this->input->post('assembly'),
						'cat_material'				=>$this->input->post('material'),
						'cat_color'					=>$this->input->post('color'),
						'cat_size'					=>$this->input->post('size'),
						'update_date'				=> date('Y-m-d H:i:s',now())
			);
			$this->admin_model->updateProductIdM($data,$id);
			redirect('addmincont/index');
		}
	}
	public function delProductC($ref= NULL){
		if($this->checkLogin()){
			$data = $this->admin_model->delPId($this->input->post('area'));
			echo $data;die();
		}
	}
	public function delProductM($ref= NULL){
		if($this->checkLogin()){
			$this->admin_model->delPIdM($ref);
		}
	}
	public function editcate(){
		
		if($this->checkLogin()){
			$data['cattype'] = $this->admin_model->getType();
			$this->my_layout->view('admin/editcat',$data);
		}
	}
	public function editcatesub(){
		if($this->checkLogin()){
			
			$data = $this->admin_model->getType();
			$couttype = count($data);
			for($i=0;$i<$couttype;$i++){
				$temp = $this->admin_model->getTypeSub($data[$i]['cat_type_id']);
				$data[$i][]=$temp;
			}
			$setview['catetype']=$data;
			$setview['error'] ='';
			$this->my_layout->view('admin/editcatsub',$setview);
		}
	}
	public function updatecat(){
		if($this->checkLogin()){
			$data = $this->admin_model->updateCatId($this->input->post('ref'), $this->input->post('name'));
			echo $data;die();
		}
	}
	public function delcatesub(){
		if($this->checkLogin()){
			$data = $this->admin_model->deleteCatId($this->input->post('ref'));
			echo $data;die();
		}
	}
	public function updatecatsub(){
		if($this->checkLogin()){
			$data = $this->admin_model->updateCatSubId($this->input->post('ref'), $this->input->post('name'));
			echo $data;die();
		}
	}
	public function delcatesubsub(){
		if($this->checkLogin()){
			$data = $this->admin_model->deleteCatSubId($this->input->post('ref'));
			echo $data;die();
		}
	}
	public function manmember(){
		if($this->checkLogin()){
			$data['member'] = $this->admin_model->getMember();
			$this->my_layout->view('admin/manage',$data);
		}
	}
	public function updateMember(){
		if($this->checkLogin()){
			$pass = md5('ThanhNV'.$this->input->post('name'));
			$data = $this->admin_model->updateMemberPass($this->input->post('ref'),$pass);
			echo $data;die();
		}
	}
	public function denyMember(){
		if($this->checkLogin()){
			$data = $this->admin_model->updateMemberDeny($this->input->post('ref'));
			echo $data;die();
		}
	}
	public function allowMember(){
		if($this->checkLogin()){
			$data = $this->admin_model->updateMemberAllow($this->input->post('ref'));
			echo $data;die();
		}
	}
	public function changhome(){
		if($this->checkLogin()){
			$data['images'] = $this->admin_model->getHomeImage();
			$this->my_layout->view('admin/edithome',$data);
		}
	}
	public function editImgHome($ref=null){
		if($this->checkLogin()){
			$data['error']='';
			$data['imgid'] = $ref;
			$this->my_layout->view('admin/editimghome',$data);
		}
	}
	public function changeImageHome(){
		if($this->checkLogin()){
			$config['upload_path'] = $this->path;
			$config['allowed_types'] = 'gif|jpg|png';
			$config['max_size']	= '100';
			$config['max_width']  = '1024';
			$config['max_height']  = '768';
	
			$this->load->library('upload', $config);
			$this->upload->initialize($config);
			if (! $this->upload->do_upload()){
				$error = array('error' => $this->upload->display_errors());
	
				$this->my_layout->view('admin/editimghome',$error);
			}
			else{
				$data1 = array('upload_data' => $this->upload->data());
				$data = array(
								'id' => $this->input->post('imgid'),
								'delete_flag' => 0,
								'images' => $data1['upload_data']['file_name']
				);
				$this->admin_model->updateImgHome($data);
				$setview['error']='Update images';
				$this->my_layout->view('admin/addsuccess', $setview);
			}
		}
	}
}